#!/bin/bash
# $Id$
#

load_env ()
{
. /etc/scripts/env
. /etc/scripts/common.fn
. /etc/scripts/common.disks.fn
. /etc/scripts/vault.fn
. /etc/scripts/disk.fn
. /etc/scripts/lvm.fn
. /etc/scripts/mountpoint.fn
. /etc/scripts/md.fn
. /etc/scripts/enclayer.fn
. /etc/scripts/packages.fn
. /etc/scripts/gui-text-common.fn
. /etc/scripts/network.fn
. /etc/scripts/time.fn
. /etc/scripts/boot.fn
. /etc/scripts/netinstall.fn
. /etc/scripts/misc.fn
. /etc/scripts/services.fn
. /etc/scripts/laststep.fn
}

export PATH="$PATH:/etc/scripts:/bin:/usr/bin:/sbin:/usr/sbin:."

load_env

# dont blank terminal
echo -e "\e[9;0]"

# trap
_chk_err ()
{
 	echo " ### ERROR ### FUNC:$2:ERR:$1"
	sleep 5
	reboot
}

_chk_exit ()
{
	echo " ### EXIT ### FUNC:$2:ERR:$1"
	sleep 5
	reboot
}

trap '_chk_err $? $FUNCNAME' ERR

export -f _chk_err
export -f _chk_exit
set -o pipefail
set -eET


_from_bdev ()
{
	# file={/dev/sdg2|usb};/path/to/file.tar.gz
	for i in $(cat /proc/cmdline); do
		if [ $(echo $i|grep "cfgfile") ]; then
			local bdev="$(echo $i|cut -f2 -d\=|cut -f1 -d\:)"
			local file="$(echo $i|cut -f2 -d\=|cut -f2 -d\:)"
			if [ "$bdev" = "usb" ]; then
				local devn=$(lookfor-sd-devs.sh)
				if ! grep -q "/mnt/tmpdev" /proc/mounts ; then
					for fs in iso9660 vfat ext2 ext3 reiserfs xfs jfs; do
  					__bin_mount -t $fs -o ro /dev/${devn} /mnt/tmpdev >/dev/null 2>&1 && break
					done
				fi
			else
				local devn=$bdev
				if ! grep -q "/mnt/tmpdev" /proc/mounts ; then
					for fs in iso9660 vfat ext2 ext3 reiserfs xfs jfs; do
  					__bin_mount -t $fs -o ro ${devn} /mnt/tmpdev >/dev/null 2>&1 && break
					done
				fi
			fi
		fi
	done

  if [ -d /nemonico ]; then
  	mv /nemonico /nemonico.org
		mkdir /nemonico
  fi

	cp /mnt/tmpdev/$file /tmp
	localfilepath=/tmp/$file

	umount /mnt/tmpdev
}

_from_net ()
{
 	#/proc/cmdline
  for i in $(cat /proc/cmdline); do
  	if [ $(echo $i|grep "net") ]; then
  		netcfg_module=$(echo $i|cut -f2 -d\=|cut -f1 -d\;)
  		netcfg_ip=$(echo $i|cut -f2 -d\=|cut -f2 -d\;)
  		netcfg_netmask=$(echo $i|cut -f2 -d\=|cut -f3 -d\;)
  		netcfg_gateway=$(echo $i|cut -f2 -d\=|cut -f4 -d\;)
  	elif [ $(echo $i|grep "srv") ]; then
  		installsrv_ip=$(echo $i|cut -f2 -d\=|cut -f1 -d\;)
  		installsrv_path=$(echo $i|cut -f2 -d\=|cut -f2 -d\;)
  	elif [ $(echo $i|grep "vp") ]; then
  		vaultp=$(echo $i|cut -f2 -d\=)
  	fi
  done	
  
 # test 
  if [ ! "$netcfg_ip" ]; then
  	echo " Error missing parameters ( n ) ...5 beeps/seconds then reboot."
  	while true; do
  		echo -ne '\a'
  		sleep 1
			count=$(($count+1))
  		test $count -eq 5 && reboot
  	done
  fi
  if [ ! "$installsrv_ip" -o "$installsrv_path" ]; then
  	echo " Error missing parameters ( installsrv ) ...3 beeps/seconds then reboot."
  	while true; do
  		echo -ne '\a'
  		sleep 1
			count=$(($count+1))
  		test $count -eq 3 && reboot
  	done
  fi
  
  # netcfg
	if [ "$netcfg_module" ]; then
		__bin_modprobe $netcfg_module
	fi
  if [ $netcfg_ip = "dhcp" ]; then
  	__bin_udhcpc -i eth0 -p /var/run/udhcpc.pid		
  else
  	__bin_ifconfig eth0 $netcfg_ip netmask $netcfg_netmask 	
  	__bin_route add default gw $netcfg_gateway
  fi

  # installsrv
  if [ -d /nemonico ]; then
  	mv /nemonico /nemonico.org
		mkdir /nemonico
  fi
	#
	# installsrv_ip = user:pass@ipnumber
	localfilepath=/tmp/"`basename $installsrv_path`"
  __bin_wget -O $localfilepath ftp://${installsrv_ip}/${installsrv_path}
}

# param: none
# desc: autoinstall 
# note: called from init; vars from here get exported
autoinstall ()
{
  export interactive_session=0

	local localfilepath=""

	_msg "\nPerforming autoinstall ...\n"

	if [ "$(cat /proc/cmdline|grep "srv=")" ]; then
		_from_net
	elif [ "$(cat /proc/cmdline|grep "cfgfile=")" ]; then
		_from_bdev
	fi
  
  if [ ! "$(tar -tzf $localfilepath)" ]; then
  	echo " Error reading archive. 2 beeps/seconds then poweroff... "
  	while true; do
  		echo -ne '\a'
  		sleep 1
			count="$(($count+1))"
  		test $count -eq 2 && reboot
  	done
  else
  	tar -mxzf $localfilepath -C /nemonico
		# lets test
		_validate_autoinstall /nemonico/AUTOINSTALL
  fi
  
  # vaultp ( vault password )
	__sane_kdir
  echo $vaultp > $vp_pass
	__sane_kdir
	hostname "$(__get_desc_val /nemonico/NETWORK HOSTNAME)"

	. /nemonico/AUTOINSTALL

	if [ "$LOCK_CONSOLE" = "Y" ]; then
		trap '' INT
		# XXX cant tell if it works or not
		chmod 000 /dev/console
		chmod 000 /dev/tty*
	fi

	# do
	for do in $DO_CMD_ORDER ; do
		if [ "$do" = "do_packages" ]; then
			"$do" batch
		elif [ "$do" = "do_misc" ]; then
			continue
		elif [ "$do" = "do_netinstall" -a ! "$(cat /proc/cmdline|grep "cfgfile=")" ]; then
			continue
		elif [ "$do" = "installer_update.sh" ]; then
			"$do"
			load_env
  		export interactive_session=0
		else
			"$do"
		fi
	done

	#
	if [ "$REBOOT" = "Y" ]; then
		echo " Autoinstall Done...rebooting ..."
		sync
		reboot
	else
		echo " Autoinstall Done...NOT rebooting ...will panic after pressing enter ..."
		sync
		read nothing
	fi
}

# vim: ts=2 syn=sh
# autoinstall.fn: end
